package cc.telecomdigital.tdstock.trading.Common;

import java.util.ArrayList;
import java.util.List;

import cc.telecomdigital.tdstock.trading.Services.TradeAppInfo;
import cc.telecomdigital.tdstock.trading.Tools.Trade_StockFunctionTools;

//Data is as accurate as the moment you have requested
public class Trade_StockPortfolioRecord
{
	public  String					accountCode;
	// The Total Stock worth based on previous Close
	public  String					closeBalance;
	public  String					purchasePowerBalance;
	
	private String					HKDStockBalance;
	private String					USDStockBalance;
	private String                     CNYStockBalance;
	private String					HKDCash;	
	private String                     USDCash;
	private String                     CNYCash;

	private List<Trade_StockHoldItem>	stockHoldList;

	/** List Item for GetProfolio **/
	public Trade_StockPortfolioRecord( String iCloseBalance, String iPurchaseBalance,
								String iHKDCash, String iUSDCash, String iCNYCash)
	{
		SetStockPorfolioAccountCode("");
		SetStockPorfolioBalance( iCloseBalance, iPurchaseBalance,
						     iHKDCash, iUSDCash, iCNYCash);
		stockHoldList = new ArrayList<Trade_StockHoldItem>();
		HKDStockBalance = "";
		CNYStockBalance = "";
		USDStockBalance = "";		
	}

	/** Calulate all the available stock balane in the same currency **/
	private void CalculateStockBalance()
	{
		HKDStockBalance = CalculateStockBalance(TradeAppInfo.HKDCode);		
		CNYStockBalance = CalculateStockBalance(TradeAppInfo.CNYCode);
		USDStockBalance = CalculateStockBalance(TradeAppInfo.USDCode);		
	}
	
	//Provide a sum o fhte calucation with the same currency type
	private String CalculateStockBalance(String iCurrencyType)
	{
		int listCount, index;
		String summation;

		if (stockHoldList == null)
			return "";

		listCount = stockHoldList.size();
		if (listCount == 0)
			return "";

		Trade_StockHoldItem curItem;
		
		summation = "0.00";
		for (index = 0; index < listCount; index++)
		{
			curItem = stockHoldList.get(index);
			if (curItem.currencyType.equals(iCurrencyType) == false)
				continue;
			
			if (curItem.IsShortSale())
				summation = Trade_StockFunctionTools.CalculateDiffInPrice(summation, curItem.totalWorth);
			else
				summation = Trade_StockFunctionTools.CalculateSumInPrice(summation, curItem.totalWorth);			
		}			

		return summation;
	}	

	/** Retrieve the raw value of HKD Cash Balance
	 *  return a non-null string, default value is 0.00 **/
	public String GetHKDCashBalance()
	{
		String cashValue;
		
		cashValue = HKDCash;
		if (cashValue == null)
			return "0.00";

		if (cashValue.equals("0"))
			return "0.00";
		
		return cashValue;
	}

	/** Retrieve the raw value of CNY Cash Balance
	 *  return a non-null string, default value is 0.00 **/	
	public String GetCNYCashBalance()
	{
		String cashValue;
		
		cashValue = CNYCash;
		if (cashValue == null)
			return "0.00";

		if (cashValue.equals("0"))
			return "0.00";
		
		return cashValue;
	}
	
	/** Retrieve the raw value of USD Cash Balance
	 *  return a non-null string, default value is 0.00 **/	
	public String GetUSDCashBalance()
	{
		String cashValue;
		
		cashValue = USDCash;
		if (cashValue == null)
			return "0.00";

		if (cashValue.equals("0"))
			return "0.00";
		
		return cashValue;
	}	
	
	/** Get the calculated value for the HKD Stock, it would be a raw number, with no format,
	 *  If no value, the string could be null or "0" **/
	public String GetHKDStockBalance()
	{
		String stockValue;
		
		stockValue = HKDStockBalance;
		if ((stockValue == null) || (stockValue.length() == 0))
			return "0.00";

		if (stockValue.equals("0"))
			return "0.00";	
		
		return stockValue;
	}

	/** Get the calculated value for the CNY Stock, it would be a raw number, with no format,
	 *  If no value, the string could be null or "0" **/	
	public String GetCNYStockBalance()
	{
		String stockValue;
		
		stockValue = CNYStockBalance;
		if ((stockValue == null) || (stockValue.length() == 0))
			return "0.00";

		if (stockValue.equals("0"))
			return "0.00";	
		
		return stockValue;
	}

	/** Get the calculated value for the USD Stock, it would be a raw number, with no format,
	 *  If no value, the string could be null or "0" **/	
	public String GetUSDStockBalance()
	{
		String stockValue;
		
		stockValue = USDStockBalance;
		if ((stockValue == null) || (stockValue.length() == 0))
			return "0.00";

		if (stockValue.equals("0"))
			return "0.00";	
		
		return stockValue;
	}		
	
	/** Directly copy all related information into the new class
	 *  Stock Balance will not be recalculated **/
	public Trade_StockPortfolioRecord(Trade_StockPortfolioRecord iOldItem)
	{
		accountCode = iOldItem.accountCode;
		SetStockPorfolioBalance( iOldItem.closeBalance, iOldItem.purchasePowerBalance,
							iOldItem.HKDCash, 	   iOldItem.USDCash, iOldItem.CNYCash);
		
		HKDStockBalance = iOldItem.HKDStockBalance;
		CNYStockBalance = iOldItem.CNYStockBalance;
		USDStockBalance = iOldItem.USDStockBalance;
		
		stockHoldList = new ArrayList<Trade_StockHoldItem>();
		stockHoldList.addAll(iOldItem.stockHoldList);
	}

	/** Clear all records for the portfolioe, including cash balance, stock balance and the stock list **/
	public void ClearRecord()
	{
		accountCode = "";
		closeBalance = "";
		purchasePowerBalance = "";
		HKDCash = "";		
		USDCash = "";
		CNYCash = "";
		HKDStockBalance = "";
		CNYStockBalance = "";
		USDStockBalance = "";		
		stockHoldList.clear();
	}

	/** Set the Portfolio Account Number, usually is the user name **/
	public void SetStockPorfolioAccountCode(String iAccountCode)
	{
		accountCode = iAccountCode;
	}

	/** When this is called, it will set all the money related value into the memory **/
	public void SetStockPorfolioBalance( String iCloseBalance, String iPurchaseBalance, 
								  String iHKDCash, String iUSDCash, String iCNYCash)
	{
		closeBalance = iCloseBalance;
		purchasePowerBalance = iPurchaseBalance;
		HKDCash = iHKDCash;		
		USDCash = iUSDCash;
		CNYCash = iCNYCash;
	}

	/** Retrieve a list of Stock Record, the new list is new created and will not
	 *  affect the internal storage
	 *  @return
	 */
	public List<Trade_StockHoldItem> GetStockRecordList()
	{
		List<Trade_StockHoldItem> newList;

		newList = new ArrayList<Trade_StockHoldItem>();
		newList.addAll(stockHoldList);
		return newList;
	}

	/** Get the List Array Pointer which stores the record for the Stock Item **/
	public List<Trade_StockHoldItem> GetStockRecordListPointer()
	{
		return stockHoldList;
	}

	/** Get the number Count for the record stored in the list **/
	public int GetStockRecordCount()
	{
		if (stockHoldList == null)
			return 0;

		return stockHoldList.size();
	}

	/** Place the provided list to override the internal stock list **/
	public void SetStockHoldList(List<Trade_StockHoldItem> iItemList)
	{
		stockHoldList.clear();
		HKDStockBalance = "";
		CNYStockBalance = "";
		USDStockBalance = "";		
		if ((iItemList == null) || (iItemList.size() == 0))
			return;

		stockHoldList.addAll(iItemList);
		// Recalculate the value
		if (iItemList.size() > 0)
			CalculateStockBalance();
	}

	/** Add the item into the stock list, and recalculate the stock value
	 * @param iItem  - the stock item
	 * @return int   - the count of the current list
	 */
	// Return the count of the current list
	public int AddStockRecordToTheList(Trade_StockHoldItem iItem)
	{
		if (stockHoldList == null)
			return -1;

		if (iItem == null)
			return -1;

		stockHoldList.add(new Trade_StockHoldItem(iItem));
		if (iItem.currencyType.equals(TradeAppInfo.CNYCode))
			CNYStockBalance = CalculateStockBalance(TradeAppInfo.CNYCode);
		else if (iItem.currencyType.equals(TradeAppInfo.USDCode))
		{
			USDStockBalance = CalculateStockBalance(TradeAppInfo.USDCode);
			if (USDStockBalance.equals("0.00") || USDStockBalance.equals(""))
				USDStockBalance = "";
		}
		else
			HKDStockBalance = CalculateStockBalance(TradeAppInfo.HKDCode);

		return stockHoldList.size();
	}
}

